package com.audaque.springboot.foshanupload.mp.util;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.apache.ibatis.reflection.property.PropertyNamer;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @author Jin
 * @date 2021/11/23
 */
public class LambdaUtils {
    private LambdaUtils() {
        throw new IllegalStateException("Utility class");
    }


    /**
     * 指定key去重
     *
     * @param extractor
     * @param <T>
     * @return
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> extractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>(16);
        return t -> seen.putIfAbsent(extractor.apply(t), Boolean.TRUE) == null;
    }


    public static <T> String columnToString(SFunction<T, ?> column) {
        return StrUtil.toUnderlineCase(columnToCamelString(column));
    }

    public static <T> String columnToCamelString(SFunction<T, ?> column) {
        LambdaMeta meta = com.baomidou.mybatisplus.core.toolkit.LambdaUtils.extract(column);
        return PropertyNamer.methodToProperty(meta.getImplMethodName());
    }


}
